/*
	Copyright (C) 2013 Pontificia Universidad Catolica del Peru
	contact: http://avatar.inf.pucp.edu.pe/realidad-aumentada/
	address contact: Av. Universitaria 1801, San Miguel, Lima-Peru.
	
	This file is part of UbicAR PUCP.

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with This program.  If not, see <http://www.gnu.org/licenses/>.
*/
/*
	Authors: Alvaro Roger Agüero Sánchez, Rodolfo Javier Galvez Meza
	Pontificia Universidad Catolica del Peru
	Departamento de Ingeniería
*/

package org.grupoavatar.guiaubicua.auxiliares;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;

import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.grupoavatar.guiaubicua.VInicial;
import org.grupoavatar.guiaubicua.entidades.ARMarker;
import org.grupoavatar.guiaubicua.mix.data.Json;
import org.grupoavatar.guiaubicua.mix.data.ResultadoConsultaRating;
import org.grupoavatar.guiaubicua.punto.TabGeneral;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.util.Log;

public class HiloConsultaRating extends AsyncTask<URL, Void, Void> {

	private TabGeneral context;
	
	public HiloConsultaRating(TabGeneral tabGeneral) {
		// TODO Auto-generated constructor stub
		context = tabGeneral;
	}

	@Override
	 protected void onPostExecute(Void url) {
		context.actualizar_rating(); 
	 }
	
	@Override
	protected Void doInBackground(URL... urls) {
		// TODO Auto-generated method stub
		Log.d(VInicial.TAG + "-Rating", "Se empieza a ejecutar el hilo");
		enviarRaiting();		
		return null;
	}
	
	protected void enviarRaiting() {
		// TODO Auto-generated method stub
		ARMarker poi = context.getPoi();		
				String url = Utiles.getConsultarRatingURL(poi.getID());				
				try {
					HttpResponse respuesta = makeRequest(url);
					if (respuesta != null) {
						String result = EntityUtils.toString(respuesta.getEntity());
						Json jsonObject = new Json();
						try {
							JSONObject root = new JSONObject(result);
							ResultadoConsultaRating resultadoObject = jsonObject.processRespuestaRating(root);
							context.resultado = resultadoObject;
							System.out.println("respuesta:");
							System.out.println(result);
							try {
								poi.setPromedio(resultadoObject.getPromedio());
							} catch(Exception e) {
								e.printStackTrace();
							}
						} catch (Exception e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}						
	}	
	
	public static HttpResponse makeRequest(String uri) {
	    try {
	        //HttpPost httpPost = new HttpPost(uri);
	        HttpGet httpGet = new HttpGet(uri);
	        /*
	        httpPost.setEntity(new StringEntity(json));
	        httpPost.setHeader("Accept", "application/json");
	        httpPost.setHeader("Content-type", "application/json");
	        return new DefaultHttpClient().execute(httpPost);
	        */
	        return new DefaultHttpClient().execute(httpGet);
	    } catch (UnsupportedEncodingException e) {
	        e.printStackTrace();
	    } catch (ClientProtocolException e) {
	        e.printStackTrace();
	    } catch (IOException e) {
	        e.printStackTrace();
	    }
	    return null;
	}

}
